//
//  YYT_ChannelViewController.m
//  AudioVisualFeast
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "QL_ChannelViewController.h"
#import "FeSpinnerTenDot.h" // 开场动画
#import "QL_ChannelModle.h"
#import "QL_ChannelView.h"
#import "QL_ChannelItem1ViewController.h"
#import "QL_ChannelItem2ViewController.h"
#import "QL_MySearchViewController.h"

@interface QL_ChannelViewController ()<FeSpinnerTenDotDelegate,QL_ChannelViewDelegate>
#pragma mark  开场动画
{
    NSInteger index;
}

@property (strong, nonatomic) FeSpinnerTenDot *spinner;
@property (strong, nonatomic) NSArray *arrTitile;
@property (strong, nonatomic) NSTimer *timer;


@property (nonatomic,retain)NSMutableArray *channelDataSource;
@property (nonatomic,retain)UICollectionView *collection;
@property (nonatomic, retain)QL_ChannelModle *channelmodel;
@property (nonatomic, retain)QL_ChannelView *Vview;
@end

@implementation QL_ChannelViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.collection];
    // 初始化频道页面数组
    self.channelDataSource = [NSMutableArray arrayWithCapacity:0];
    //    导航栏文字
    self.navigationItem.title = @"频道";
    //    设置导航栏文字颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    //    首页左侧抽屉图片点击方法
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"MVfanhui"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(left1:)];
    //    首页右侧搜索图片点击方法
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"Liebiaoyou"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
    //    导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    //    半透明效果
    self.navigationController.navigationBar.translucent = YES;
    
#pragma mark  animation
    // 定义index的值
    index = 0;
    // 开场动画下面伴随的文字数组
    self.arrTitile = [NSArray arrayWithObjects:@"正在进入频道", nil];
    _spinner = [[FeSpinnerTenDot alloc] initWithView:self.view withBlur:NO];
    _spinner.titleLabelText = _arrTitile[index];
    _spinner.delegate = self;
    [self.view addSubview:_spinner];
    
    self.Vview = [[QL_ChannelView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT)];
    self.Vview.channelDelegate = self;
    [self.view addSubview:_Vview];
    
    
}

- (void)qiao1:(NSString *)ming1 shu1:(NSInteger)shu1{
    QL_ChannelItem1ViewController *item1 = [[QL_ChannelItem1ViewController alloc]init];
    item1.biaoti = ming1;
    item1.chuanzhi = shu1;
    [self.navigationController pushViewController:item1 animated:YES];
    
}
- (void)qiao2:(NSString *)ming2 shu2:(NSInteger)shu2{
    QL_ChannelItem2ViewController *item2 = [[QL_ChannelItem2ViewController alloc]init];
    item2.biaoti = ming2;
    item2.chuanzhi = shu2;
    [self.navigationController pushViewController:item2 animated:YES];
}

#pragma mark  animation delegate
-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self start:self];
    //    [self performSelector:@selector(dismiss:) withObject:nil afterDelay:7.0f];
}

- (void)start:(id)sender {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTitle) userInfo:nil repeats:YES];
    }
    [_spinner showWhileExecutingSelector:@selector(longTask) onTarget:self withObject:nil completion:^{
        [_timer invalidate];
        _timer = nil;
        index = 0;
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
}

-(void) longTask {
    // 2秒后异步执行网络请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 动画中 加载数据
        [self getData];
    });
    sleep(2);
}

- (void)getData {
    [QL_AFNTools getWithUrl:@"http://mapi.yinyuetai.com/recommend/video/aggregation.json" parameter:nil httpHeader:@{@"App-Id":@"10101026",@"Device-Id":@"587d897556663eb999e31a4c621abfbc",@"Device-V":@"aVBob25lIE9TXzkuMl82NDAqMTEzNl8xMDAwMDEwMDBfaVBob25lNiwy"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dictionary = [[NSDictionary alloc] initWithDictionary:result];
        NSArray *array = dictionary[@"data"];
        for (NSDictionary *dic in array) {
            QL_ChannelModle *model = [[QL_ChannelModle alloc] initWithDictionary:dic];
            [self.channelDataSource addObject:model];
            
        }
       self.Vview.chArray = self.channelDataSource;
        [self.Vview.dingyue reloadData];
        
    } fail:^(NSError *error) {
        
        
    }];
}


- (void)dismiss:(id)sender {
    [_spinner dismiss];
}

-(void) changeTitle {
    if (index >= _arrTitile.count)
        return;
    _spinner.titleLabelText = _arrTitile[index];
    index++;
}

- (void)right:(UIButton *)sender {
    QL_MySearchViewController *searchVC = [[QL_MySearchViewController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)left1:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
